//
//  VisitorView.swift
//  武汉微博
//
//  Created by 谌桃 on 16/9/21.
//  Copyright © 2016年 ryan.chen. All rights reserved.
//

import UIKit
import SnapKit

//定义协议
protocol visitorViewDelegate:NSObjectProtocol{

    func visitorViewRegisterButtonClick()
    
    func visitorViewLoginButtonClick()
}

class VisitorView: UIView {
    
    // MARK: - 定义登录和注册 代理属性
    weak var delegate:visitorViewDelegate?

    override  init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
        //为 注册和登录 注册事件
        registerButton.addTarget(self, action: "registerButtonClick", forControlEvents: .TouchUpInside)
        
        loginButton.addTarget(self, action: "loginButtonClick", forControlEvents: .TouchUpInside)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 调用注册和登录 代理方法
    @objc private func registerButtonClick(){
    
        print("点击注册")
        delegate?.visitorViewRegisterButtonClick()
    }
    
    @objc private func loginButtonClick(){
    
        print("点击登陆")
        delegate?.visitorViewLoginButtonClick()
    }
    
    // MARK: - 设置访客视图信息
    func setupInfo(imageName:String? = nil, message:String){
    
        messageLabel.text = message
        
        //图片名称为空，标示进入首页
        guard let imageName = imageName else{
            
            //首页开启动画
            startAnimation()
        
            return
        }
        
        maskIconView.hidden = true
        homeView.hidden = true
        
        iconView.image = UIImage(named: imageName)
    }
    
    // MARK: - 首页访客视图动画
    private func startAnimation(){
    
        //开启基本动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        
        //对于需要循环播放的动画，可以设置此属性为false，当动画所在的视图销毁的销毁的时候，动画才会被销毁，可以达到一致循环播放
        anim.removedOnCompletion = false
        
        iconView.layer.addAnimation(anim, forKey: nil)
        
    }
    
    // MARK: - 私有控件
    //图标视图
    private lazy var iconView:UIImageView = UIImageView(imageNameStr: "visitordiscover_feed_image_smallicon")
    //小房子视图
    private lazy var homeView:UIImageView = UIImageView(imageNameStr: "visitordiscover_feed_image_house")
    //遮罩视图
    private lazy var maskIconView:UIImageView = UIImageView(imageNameStr: "visitordiscover_feed_mask_smallicon")
    //消息标签
    private lazy var messageLabel:UILabel = UILabel(textStr: "关注一些人，回来看看这里有什么惊喜")
    
    //注册按钮
    private lazy var registerButton:UIButton = UIButton(titleName:  "注 册", titleColor: UIColor.orangeColor(), backImageName: "common_button_white_disable")
    
    //登陆按钮
    private lazy var loginButton:UIButton = UIButton(titleName: "登 录", titleColor: UIColor.orangeColor(), backImageName: "common_button_white_disable")
}

 // MARK: - 设置界面
extension VisitorView{

    private func setupUI(){
//        backgroundColor = UIColor.brownColor()
        
        addSubview(iconView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        addSubview(maskIconView)
        
        for v in subviews{
        
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //VFL:可视化格式语言， 设置居中非常麻烦
        //苹果原生的自动布局
        //firstItem.firstAttribute {==,<=,>=} secondItem.secondAttribute * multiplier + constant
        //一个视图.属性 ＝ 另一个视图的.属性 ＊ 乘积 ＋ 约束值
        
        //图标视图
        self.addConstraint(NSLayoutConstraint(
            item: iconView,
            attribute:  .CenterX,
            relatedBy: .Equal,
            toItem: self,
            attribute: .CenterX,
            multiplier: 1.0,
            constant: 0.0))
        
        self.addConstraint(NSLayoutConstraint(
            item: iconView,
            attribute:  .CenterY,
            relatedBy: .Equal,
            toItem: self,
            attribute: .CenterY,
            multiplier: 1.0,
            constant: -60.0))
        
        //小房子视图
        self.addConstraint(NSLayoutConstraint(
            item: homeView,
            attribute:  .CenterX,
            relatedBy: .Equal,
            toItem: iconView,
            attribute: .CenterX,
            multiplier: 1.0,
            constant: 0.0))
        
        self.addConstraint(NSLayoutConstraint(
            item: homeView,
            attribute:  .CenterY,
            relatedBy: .Equal,
            toItem: iconView,
            attribute: .CenterY,
            multiplier: 1.0,
            constant: 0.0))
        
       
        
        //消息标签
        self.addConstraint(NSLayoutConstraint(
            item: messageLabel,
            attribute:  .CenterX,
            relatedBy: .Equal,
            toItem: homeView,
            attribute: .CenterX,
            multiplier: 1.0,
            constant: 0.0))
        
        self.addConstraint(NSLayoutConstraint(
            item: messageLabel,
            attribute:  .Top,
            relatedBy: .Equal,
            toItem: homeView,
            attribute: .Bottom,
            multiplier: 1.0,
            constant: 60.0))
        
        self.addConstraint(NSLayoutConstraint(
            item: messageLabel,
            attribute: .Width,
            relatedBy: .Equal,
            toItem: self,
            attribute: .Width,
            multiplier: 0.8,
            constant: 0))
        
        //登陆按钮
        self.addConstraint(NSLayoutConstraint(
            item: loginButton,
            attribute:  .CenterX,
            relatedBy: .Equal,
            toItem: messageLabel,
            attribute: .CenterX,
            multiplier: 1.0,
            constant: -60.0))
        
        self.addConstraint(NSLayoutConstraint(
            item: loginButton,
            attribute:  .CenterY,
            relatedBy: .Equal,
            toItem: messageLabel,
            attribute: .CenterY,
            multiplier: 1.0,
            constant: 60.0))
        
        //注册按钮
        self.addConstraint(NSLayoutConstraint(
            item: registerButton,
            attribute:  .CenterX,
            relatedBy: .Equal,
            toItem: messageLabel,
            attribute: .CenterX,
            multiplier: 1.0,
            constant: 60.0))
        
        self.addConstraint(NSLayoutConstraint(
            item: registerButton,
            attribute:  .CenterY,
            relatedBy: .Equal,
            toItem: messageLabel,
            attribute: .CenterY,
            multiplier: 1.0,
            constant: 60.0))
        
        //利用snp
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(messageLabel.snp_top)
        }
        
        //设置背景颜色
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0 )
    }
}